using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;

using wnet.admin.Models;

using System;
using wnet.admin.wentcommon;
using Models;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using wnet.admin.Models.business;
using System.Linq;

namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 角色
    /// </summary>
    //[TokenAuth]
    [Route("business/sysrole/{action}")]
    public class sysroleController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public sysroleController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }




        /// <summary>
        /// 角色-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object list(sys_menuQueryDto parm)
        {
            if (parm==null)
            {
                parm=new sys_menuQueryDto();
            }

            var predicate = Expressionable.Create<sys_role>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.name), it => it.name.Contains(parm.name));

            var query = _sqlSugarClient.Queryable<sys_role>()
                .Where(predicate.ToExpression())
                .Select((u) => new sys_role
                {
                    id = u.id.SelectAll(),

                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 角色-详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getsys_role(int id)
        {
            var response = _sqlSugarClient.Queryable<sys_role>()
                .Where(q => q.id == id).First();

            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 角色-添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Addsys_role([FromBody] sys_role parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Insertable<sys_role>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 角色-编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Updatesys_role([FromBody] sys_role parm)
        {

            parm.createtime = DateTime.Now;

            var response = _sqlSugarClient.Updateable<sys_role>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 角色-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Deletesys_role(int id)
        {
            try
            {
                var response = _sqlSugarClient.Queryable<sys_role>()
                     .Where(q => q.id == id).First();
                if (response == null)
                {
                    throw new Exception("数据不存在");
                }
                _sqlSugarClient.Deleteable<sys_role>(response).ExecuteCommand();
                var data = new { code = 200, msg = "成功", data = response };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
            catch (Exception ex)
            {

                var data = new { code = -1, msg = ex.Message, data = "null" };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
        }
    }
}